package com.qztech.bluetooth;

import com.qztech.bluetooth.event.NotifyDataReadCompleteEvent;
import com.qztech.bluetooth.event.NotifyDataReadEvent;
import com.qztech.bluetooth.util.BLog;
import com.qztech.bluetooth.util.ByteUtil;
import com.qztech.utils.YzDebug;

import org.greenrobot.eventbus.EventBus;

/**
 * @author Zhanghb
 * Email: 2573475062@qq.com
 * Date : 2020-12-06 15:58
 */
public class ReceiveDataUtil {
    private static StringBuilder sData;

    public static void init() {
        sData = new StringBuilder();
    }

    public static void clear() {
        sData = null;
    }

    public static synchronized void receivedData(byte[] data) {
        String dataStr = ByteUtil.bytesToHexString(data);
        if (sData == null) {
            init();
        }
        sData.append(dataStr);
//        BLog.i("data", dataStr + " , length=" + sData.length());
        //if (BuildConfig.DEBUG)
        {
            YzDebug.d("receivedData:" + dataStr +" " + sData.length());
            EventBus.getDefault().post(new NotifyDataReadEvent(dataStr, sData.length()  ));
        }
//        if (sData.length() == 3852) {//3852,2996
//            EventBus.getDefault().post(new NotifyDataReadCompleteEvent());
//        } else
        if (sData.length() >= 8316) {//8316,6468
            EventBus.getDefault().post(new NotifyDataReadCompleteEvent());
        }
    }

    public static StringBuilder getsData() {
        return sData;
    }
}
